home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18438 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  56 lines

  1. Path: news.ruhr-uni-bochum.de!usenet
  2. From: Karsten Soete <Karsten.Soete@rz.ruhr-uni-bochum.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Return
  5. Date: 20 Apr 1996 14:43:50 GMT
  6. Organization: Ruhr-Universitaet Bochum, Rechenzentrum
  7. Message-ID: <4lat76$hh3@sun168.rz.ruhr-uni-bochum.de>
  8. NNTP-Posting-Host: dialslip-39.rz.ruhr-uni-bochum.de
  9.  
  10. morrisd@nevada.edu (DAMON MORRIS) schreibt:
  11. > I am having a really bad problem with some return statements...
  12. > I declare a function to return an int value then in the function
  13. > I check to see if two strings are equal and if so then I use another
  14. > variable to see what to return...
  15. > int charisma(int ability, char *choice)
  16. >  {
  17. >   if (choice == "Loyalty Base")
  18. >    {
  19. >     if (ability==10) return 20;
  20. >    }
  21. >  }
  22. > This little program will never work for some reason. When I call it
  23. > like this charisma(10,"Loyalty Base") it always returns some number
  24. > that it shouldn't. The number that is returned is usually between
  25. > 5000-10000. If I wrote the value of choice to the screen before it
  26. > checked to see if it was equal to "Loyalty Base" it always says that
  27. > it is equal to "Loyalty Base", yet it never gets to the second if.
  28. > I have no idea what is the matter...I will send anyone full source if
  29. > needed to fix the problem, but the source is 1000+ lines....
  30.  
  31. First the equal-operator (==) must be implented for char *.
  32. This is the case only for string-classes and I think it doesn't
  33. apply here. To compare to strings conventionally use strcmp.
  34.  
  35. I your case a simple pointer-comparation between choice and the
  36. memory-location of "Loayalty Base" is done. This won't work.
  37.  
  38. So the comparation never matches and the return-value in the
  39. if-statement can't be returned.
  40. By the way, what then. Your function doesn't define a return-value
  41. in case the comparation won't match. 
  42.  
  43. Try this code:
  44. #include <string.h>
  45.  
  46. int charisma(int ability, char *choice){
  47.   if(!strcmp(choice,"Loyalty Base"))
  48.     if(ability==10) return 20;
  49.  
  50.   return 10; //in case comparations don't match.
  51. }
  52.